package com.evon.yardmanagement.dao;

import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.evon.yardmanagement.exception.YardManagementException;
import com.evon.yardmanagement.exception.YardManagementException.ExceptionType;
import com.evon.yardmanagement.model.Section;

/**
 * A data access object (DAO) providing persistence and search support for
 * Section entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.abc.yardmanagement.dao.Section
 * @author MyEclipse Persistence Tools
 */

public class SectionDAO extends BaseHibernateDAO<Section> {
	private static final Log log = LogFactory.getLog(SectionDAO.class);
	// property constants
	public static final String SECTION_NAME = "sectionName";
	public static final String PERMANENT = "permanent";
	public static final String ACTIVE = "active";
	public static final String HSC_ID = "hscId";
	public static final String HSU_ID = "hsuId";

	@Override
	public Class getQueryClass() {
		// TODO Auto-generated method stub
		return Section.class;
	}

	public List findBySectionName(Object sectionName) {
		return findByProperty(SECTION_NAME, sectionName);
	}

	public List findByPermanent(Object permanent) {
		return findByProperty(PERMANENT, permanent);
	}

	public List findByActive(Object active) {
		return findByProperty(ACTIVE, active);
	}

	public List findByHscId(Object hscId) {
		return findByProperty(HSC_ID, hscId);
	}

	public List findByHsuId(Object hsuId) {
		return findByProperty(HSU_ID, hsuId);
	}

	public List<Section> getAvailableSections(int selectedYardId) {
		Criteria criteria = getSession().createCriteria(getQueryClass());
		criteria.add(Restrictions.eq("yard.id", selectedYardId));
		return criteria.list();
	}
	
	public void setActive(int yardId){
		try {
		Transaction transaction = getSession().beginTransaction();
		String stringQuery = "UPDATE Section SET isActive = 1 WHERE yard.id = :yardId";
		Query query = getSession().createQuery(stringQuery);
		query.setInteger("yardId", yardId);
		query.executeUpdate();
		transaction.commit();
		} catch (Exception re) {
			log.error("Activate section by yard id failed", re);
			throw new YardManagementException(re, ExceptionType.RECOVERABLE);
		}
		
	}
	
	public void setDeActive(int yardId){
		try {
		Transaction transaction = getSession().beginTransaction();
		String stringQuery = "UPDATE Section SET isActive = 0 WHERE yard.id = :yardId";
		Query query = getSession().createQuery(stringQuery);
		query.setInteger("yardId", yardId);
		query.executeUpdate();
		transaction.commit();
		} catch (Exception re) {
			log.error("Activate section by yard id failed", re);
			throw new YardManagementException(re, ExceptionType.RECOVERABLE);
		}
		
	}

}